﻿package org.xhs.report.common.model
{
    import baidu.ui.managers.*;
    import flash.events.*;
    import flash.net.*;

    public class RelativeModel extends EventDispatcher
    {
        protected var _hasData:Boolean = false;
        protected var _isOpen:Boolean = false;
        protected var relativeLoader:URLLoader;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var _dataArray:Array;
        protected var _moreUrl:String;
        public static const DATA_SUCCEED:String = "DATA_SUCCEED";
        public static const DATA_FAIL:String = "DATA_FAIL";
        public static const OPEN_PANEL:String = "OPEN_PANEL";
        public static const CLOSE_PANEL:String = "CLOSE_PANEL";

        public function RelativeModel(param1:IEventDispatcher = null)
        {
            super(param1);
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            return;
        }// end function

        protected function loadData() : void
        {
            this.relativeLoader = new URLLoader();
            this.relativeLoader.addEventListener(Event.COMPLETE, this.relativeDocLoaded);
            this.relativeLoader.addEventListener(IOErrorEvent.IO_ERROR, this.relativeDocLoaded);
            this.relativeLoader.addEventListener(IOErrorEvent.NETWORK_ERROR, this.relativeDocLoaded);
            this.relativeLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.relativeDocLoaded);
            var _loc_1:* = this.loaderInfoVo.domainUrl + "api/relate.php?doc_id=" + this.loaderInfoVo.docId;
            this.relativeLoader.load(new URLRequest(_loc_1));
            return;
        }// end function

        protected function relativeDocLoaded(event:Event) : void
        {
            var data:XML;
            var evt:* = event;
            if (evt.type == Event.COMPLETE)
            {
                try
                {
                    data = new XML((evt.target as URLLoader).data);
                    if (data && data.@m)
                    {
                        this.parseData(data);
                    }
                    else
                    {
                        dispatchEvent(new Event(DATA_FAIL));
                    }
                }
                catch (e:Error)
                {
                    dispatchEvent(new Event(e.DATA_FAIL));
                }
            }
            else
            {
                dispatchEvent(new Event(DATA_FAIL));
            }
            return;
        }// end function

        protected function parseData(param1:XML) : void
        {
            var _loc_2:Object = null;
            this._dataArray = [];
            var _loc_3:* = param1.children().length();
            this._moreUrl = param1.@m ? (param1.@m) : ("http://wenku.baidu.com");
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_2 = new Object();
                _loc_2.id = _loc_4;
                _loc_2.docType = param1.children()[_loc_4].@dt ? (param1.children()[_loc_4].@dt) : ("doc");
                _loc_2.title = param1.children()[_loc_4].@tt ? (param1.children()[_loc_4].@tt) : ("百度文库");
                _loc_2.link = param1.children()[_loc_4].@u ? (param1.children()[_loc_4].@u) : ("http://wenku.baidu.com");
                this._dataArray.push(_loc_2);
                _loc_4++;
            }
            this._hasData = true;
            if (this._isOpen)
            {
                dispatchEvent(new Event(OPEN_PANEL));
            }
            return;
        }// end function

        public function get dataArray() : Array
        {
            return this._dataArray;
        }// end function

        public function get moreUrl() : String
        {
            return this._moreUrl;
        }// end function

        public function get hasData() : Boolean
        {
            return this._hasData;
        }// end function

        public function get isOpen() : Boolean
        {
            return this._isOpen;
        }// end function

        public function set isOpen(param1:Boolean) : void
        {
            this._isOpen = param1;
            if (this._isOpen)
            {
                if (this._hasData)
                {
                    dispatchEvent(new Event(OPEN_PANEL));
                }
                else
                {
                    this.loadData();
                }
            }
            else
            {
                dispatchEvent(new Event(CLOSE_PANEL));
            }
            return;
        }// end function

    }
}
